<template>
    <div class="login">
      <van-nav-bar title="登录" left-arrow @click-left="$router.go(-1)"/>
      <div class="body">
        <div class="title">
          <h3>手机号登录</h3>
          <p>未注册的手机号登录后将自动注册</p>
        </div>
        <div class="form">
          <div class="from-item">
            <input class="inp" maxlength="11" placeholder="请输入手机号码" type="text" v-model="phone">
          </div>
          <div class="from-item">
            <input class="inp" maxlength="5" placeholder="请输入图形验证码" type="text" v-model="captchaCode">
            <img :src="picUrl" alt="" @click="getCaptcha">
          </div>
          <div class="from-item">
            <input class="inp" maxlength="11" placeholder="请输入短信验证码" type="text" v-model="msgCode">
            <button @click="getCode">{{ totalCount === currentCount ? '获取短信验证码' : currentCount }}</button>
          </div>
        </div>

        <div class="login-btn" @click="login">登录</div>
      </div>
    </div>
  </template>

<script >
import { getCaptcha, getMsgCode, login } from '@/api/login'

export default {
  name: 'LoginIndex',
  data () {
    return {
      picUrl: '',
      totalCount: 60,
      currentCount: 60,
      timer: null,
      phone: '',
      captchaCode: '',
      captchaToken: '',
      phoneToken: '',
      msgCode: '',
      hasGetMsg: false
    }
  },
  async created () {
    this.getCaptcha()
  },
  methods: {
    async getCaptcha () {
      const res = await getCaptcha()
      this.picUrl = res.data.image
      this.captchaToken = res.data.jwt
    },
    async getCode () {
      if (!this.validFn()) {
        return
      }
      if (this.timer !== null || this.totalCount !== this.currentCount) {
        return
      }
      const res = await getMsgCode(this.captchaCode, this.captchaToken, this.phone)
      this.$toast('获取短信验证码成功')
      this.hasGetMsg = true
      this.phoneToken = res.data
      this.timer = setInterval(() => {
        this.currentCount--
        if (this.currentCount === 0) {
          clearInterval(this.timer)
          this.timer = null
          this.currentCount = this.totalCount
        }
      }, 1000)
    },
    validFn () {
      if (!/^1[3-9]\d{9}$/.test(this.phone)) {
        this.$toast('请输入正确的手机号')
        return false
      }
      if (!/^\w{5}$/.test(this.captchaCode)) {
        this.$toast('请输入正确的图形验证码')
        return false
      }
      return true
    },
    async login () {
      if (!this.hasGetMsg) {
        this.$toast('请先获取验证码')
        return
      }
      if (!this.validFn()) {
        return
      }
      if (!/^\d{6}/.test(this.msgCode)) {
        this.$toast('手机验证码格式有误')
        return
      }
      const res = await login(this.phone, this.msgCode, this.phoneToken)
      this.$store.commit('user/setUserInfo', { token: res.data.token, userId: res.data.userId })
      this.$toast('登录成功')
      const url = this.$route.query.backUrl || '/'
      this.$router.replace(url)
    }
  },

  destroyed () {
    clearInterval(this.timer)
  }
}
</script>

  <style lang="less" scoped>
  .body {
    padding: 49px 29px;
  }
  .body .title {
    margin-bottom: 20px;
  }
  .body .title h2 {
    font-weight: normal;
    font-size: 26px;
  }

  .body .title p {
    line-height: 40px;
    font-size: 14px;
    color: #b8b8b8;
  }

  .from-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }
  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  </style>
